package service

import (
	"chaowan/data_source"
	"chaowan/models"
	"chaowan/redis_lib"
	"context"
	"encoding/json"
	"time"
)

func Citylist() []models.City {
	var citys []models.City
	ctx := context.Background()
	cityslist, _ := redis_lib.Client.Get(ctx, "chaowancitylist").Result()
	if cityslist == "" || len(cityslist) == 0 || cityslist == "null" {
		data_source.Db.Where("type = 1 or type = 2 or type = 3").Find(&citys)
		byte_data, _ := json.Marshal(citys)
		str := string(byte_data)
		redis_lib.Client.Set(ctx, "chaowancitylist", str, -1)
	} else {
		json.Unmarshal([]byte(cityslist), &citys)
	}
	return citys
}

func Niudanxianshou() []models.Niudansql {
	var niudans []models.Niudansql
	ctx := context.Background()
	niudanslist, _ := redis_lib.Client.Get(ctx, "chaowanniudanslist").Result()
	if niudanslist == "" || len(niudanslist) == 0 || niudanslist == "null" {
		data_source.Db.Model(&models.Niudan{}).Where("usernum > 0 or daynum > 0").Where("status = 1").Find(&niudans)
		byte_data, _ := json.Marshal(niudans)
		str := string(byte_data)
		redis_lib.Client.Set(ctx, "chaowanniudanslist", str, 86400*time.Second)
	} else {
		json.Unmarshal([]byte(niudanslist), &niudans)
	}
	return niudans
}
func Niudanxianshouupdate() {
	ctx := context.Background()
	var niudans []models.Niudansql
	data_source.Db.Model(&models.Niudan{}).Where("usernum > 0 or daynum > 0").Where("status = 1").Find(&niudans)
	byte_data, _ := json.Marshal(niudans)
	str := string(byte_data)
	redis_lib.Client.Set(ctx, "chaowanniudanslist", str, 86400*time.Second)
}

func Fanshangxianshou() []models.Fanshangsql {
	var fansahngs []models.Fanshangsql
	ctx := context.Background()
	fansahngslist, _ := redis_lib.Client.Get(ctx, "chaowanfansahngslist").Result()
	if fansahngslist == "" || len(fansahngslist) == 0 || fansahngslist == "null" {
		data_source.Db.Model(&models.Fanshang{}).Where("usernum > 0 or daynum > 0").Where("status = 1").Find(&fansahngs)
		byte_data, _ := json.Marshal(fansahngs)
		str := string(byte_data)
		redis_lib.Client.Set(ctx, "chaowanniudanslist", str, 86400*time.Second)
	} else {
		json.Unmarshal([]byte(fansahngslist), &fansahngs)
	}
	return fansahngs
}
func Fanshangxianshouupdate() {
	ctx := context.Background()
	var fansahngs []models.Fanshangsql
	data_source.Db.Model(&models.Fanshang{}).Where("usernum > 0 or daynum > 0").Where("status = 1").Find(&fansahngs)
	byte_data, _ := json.Marshal(fansahngs)
	str := string(byte_data)
	redis_lib.Client.Set(ctx, "chaowanfansahngslist", str, 86400*time.Second)
}

func Qudaolist() []models.Qudao {
	var qudaos []models.Qudao
	ctx := context.Background()
	qudaoslist, _ := redis_lib.Client.Get(ctx, "chaowanqudaoslist").Result()
	if qudaoslist == "" || len(qudaoslist) == 0 || qudaoslist == "null" {
		data_source.Db.Find(&qudaos)
		byte_data, _ := json.Marshal(qudaos)
		str := string(byte_data)
		redis_lib.Client.Set(ctx, "chaowanqudaoslist", str, 86400*time.Second)
	} else {
		json.Unmarshal([]byte(qudaoslist), &qudaos)
	}
	return qudaos
}
